package shServer;

import java.net.*;
import java.io.*;

import safehome.Packet;

public class SensorServerThread implements Runnable
{
	private Socket incoming;

	private ObjectOutputStream outStream;
	private ObjectInputStream inStream;
	
	public SensorServerThread(Socket i)
	{
		incoming = i;
		getStreams();
	}
	
	public void getStreams()
	{
		try
		{
			try
			{
				inStream = new ObjectInputStream(
						new BufferedInputStream(incoming.getInputStream()) );
				
				outStream = new ObjectOutputStream( 
						new BufferedOutputStream(incoming.getOutputStream()) );
				outStream.flush();
//				outStream.writeObject(toSend);				
			}
			finally
			{
				incoming.close();
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public void run()
	{
		Packet toSend = new Packet();
		
		
		Runnable listen = new SensorServerListen(inStream);
		Thread t1 = new Thread(listen);
		t1.run();
	}
}

class SensorServerTalk implements Runnable
{
	private Socket incoming;
	
	public SensorServerTalk(Socket i)
	{
		incoming = i;
	}
	
	public void run()
	{
		
	}
}

class  SensorServerListen implements Runnable
{
	
	private ObjectInputStream inStream;
	
	public SensorServerListen(ObjectInputStream in)
	{
		inStream = in;
	}
	
	public void run()
	{
		Packet received = new Packet();
		do
		{
			try
			{
				received = (Packet) inStream.readObject();
			}
	        catch ( ClassNotFoundException classNotFoundException )
	        {
	        	System.out.println(classNotFoundException.getCause());
	        }
	        catch (IOException e)
	        {
	        	e.printStackTrace();
	        }
	        
	        // action happened send packet off
	        
		}while(received.connected());

		// action happened tell someone the sensor is gone
		
	}
}

